package com.ptswitch.material.pages.visitor;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.slf4j.Logger;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.LoginInfoService;
import com.ptswitch.material.service.UserService;
import com.ptswitch.material.util.HlogConstants;

public class EditVisitor extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private UserService userSer;
    @Inject
    private LoginInfoService loginInfoSer;
    @InjectPage
    private Login login;

    @OnEvent(value = EventConstants.SUCCESS, component = "changePassword")
    Object changePassword() {
//		mustSelf();
        Uzer visitor = getVisitor();
        visitor.md5password();
        userSer.save(visitor);
        hlog(HlogConstants.updateUserPassword, visitor);
        logger.info("change password", visitor);
        //re login,update login info
        visitor = userSer.findByUsernameAndPassword(visitor.getUsername(), visitor.getPassword());
        if (visitor == null) {
            throw AppRuntimeException.error("重新登录失败，用户名或密码错误");
        }
        loginInfoSer.deleteByUsername(visitor.getUsername());
        return login;
    }
}
